package com.fw.ui.view.profile.merchant.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.SearchCriteriaBean;
import com.fw.service.bean.SearchResultBean;
import com.fw.service.merchantprofile.bean.MerchantProfileBean;
import com.fw.service.merchantprofile.bean.SearchMerchantProfileBean;
import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.MerchantProfileBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.bao.PropertyBAO;
import com.fw.ui.util.SessionInfoUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.common.SortingTablePanel;
import com.fw.ui.view.common.TablePanel;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.fw.ui.view.profile.SearchMerchantProfileView;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class SearchMerchantProfileSearchButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;

    @Autowired
    private MerchantProfileBAO merchantProfileBAO;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private PropertyBAO propertyBAO;
    @Autowired
    private SessionInfoUtils sessionInfoUtils;

    @SuppressWarnings("rawtypes")
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	SearchMerchantProfileView searchMerchantProfileView = (SearchMerchantProfileView) this.getBasedComponent();

	TablePanel tablePanel = searchMerchantProfileView.getTablePanel();
	SortingTablePanel sortingTablePanel = tablePanel.getSortingTablePanel();

	Integer size = tablePanel.getManualPageSize();
	int page = 1;

	String sort1 = (String) sortingTablePanel.getCbbSelectSort1().getValue();
	String sort2 = (String) sortingTablePanel.getCbbSelectSort2().getValue();
	String sort3 = (String) sortingTablePanel.getCbbSelectSort3().getValue();
	String sortType1 = (String) sortingTablePanel.getOpgSortingType1().getValue();
	String sortType2 = (String) sortingTablePanel.getOpgSortingType2().getValue();
	String sortType3 = (String) sortingTablePanel.getOpgSortingType3().getValue();

	Map<String, String> sortMap = new LinkedHashMap<String, String>();
	if (sort1 != null) {
	    sortMap.put(sort1, sortType1);
	}
	if (sort2 != null && !sortMap.containsKey(sort2)) {
	    sortMap.put(sort2, sortType2);
	}
	if (sort3 != null && !sortMap.containsKey(sort3)) {
	    sortMap.put(sort3, sortType3);
	}

	SearchMerchantProfileBean searchMerchantProfileBean = new SearchMerchantProfileBean();

	searchMerchantProfileBean.setMerchantId((String) searchMerchantProfileView.getCbbMerchantId().getValue());
	searchMerchantProfileBean.setCompanyName((String) searchMerchantProfileView.getCbbCompanyName().getValue());
	searchMerchantProfileBean.setMerchantUrl((String) searchMerchantProfileView.getCbbMerchantUrl().getValue());
	searchMerchantProfileBean.setProjectContactPerson((String) searchMerchantProfileView.getCbbProjectContactPerson().getValue());

	searchMerchantProfileBean
		.setMerchantServiceType(((searchMerchantProfileView.getCbbMerchantServiceType().getValue() == null
			|| searchMerchantProfileView.getCbbMerchantServiceType().getValue().toString().trim().length() == 0 || searchMerchantProfileView
			.getCbbMerchantServiceType().getValue().toString().trim().equals("0")) ? null : searchMerchantProfileView
			.getCbbMerchantServiceType().getValue().toString()));

	searchMerchantProfileBean.setStatus(((searchMerchantProfileView.getCbbStatus().getValue() == null
		|| searchMerchantProfileView.getCbbStatus().getValue().toString().trim().length() == 0 || searchMerchantProfileView
		.getCbbStatus().getValue().toString().trim().equals("0")) ? null : searchMerchantProfileView.getCbbStatus().getValue()
		.toString()));

	searchMerchantProfileBean.setHideMerchantTest(searchMerchantProfileView.getCbHideMerchantTest().getValue());

	ApplicationUI applicationUI = (ApplicationUI) searchMerchantProfileView.getUI();
	sessionInfoUtils.setSessionInfoDetails(searchMerchantProfileBean, applicationUI.getAuthenticationBean(), applicationUI.getLocale());

	SearchCriteriaBean<SearchMerchantProfileBean> searchCriteriaBean = new SearchCriteriaBean<SearchMerchantProfileBean>();
	if (!sortMap.isEmpty()) {
	    searchCriteriaBean.setSortingCriteria(sortMap);
	} else {
	    String sortingDefault = this.propertyBAO
		    .getPropertyValue("com.fw.ui.view.profile.SearchMerchantProfileView.default.table.sorting");
	    searchCriteriaBean.setDirection1(SearchCriteriaBean.DIRECTION_DESC);
	    searchCriteriaBean.setOrder1(sortingDefault);
	}

	searchCriteriaBean.setCriteriaBean(searchMerchantProfileBean);
	searchCriteriaBean.setPage(page);
	searchCriteriaBean.setSize(size);
	searchMerchantProfileView.setSearchCriteriaBean(searchCriteriaBean);

	SearchResultBean<MerchantProfileBean> searchResultBean = merchantProfileBAO.selectMerchantProfile(searchCriteriaBean);

	Integer totalPage = searchResultBean.getTotalPages();
	if (totalPage == null) {
	    totalPage = 1;
	}
	Long totalElement = searchResultBean.getTotalElements();
	if (totalElement == null) {
	    totalElement = 0l;
	}
	List<BeanItem> beanItems = searchMerchantProfileView.generateTableItemBean(searchResultBean);

	searchMerchantProfileView.getTablePanel().clearTableData();
	searchMerchantProfileView.getTablePanel().setManualPageTable(1);
	searchMerchantProfileView.getTablePanel().setTableBeanContainerDataTotalPage(beanItems, totalPage, totalElement);

	if (beanItems.isEmpty()) {
	    Map<String, Object> result = new HashMap<String, Object>();
	    result.put(MessageBAO.MESSAGE_INFO_TYPE, "message.search.not.found");
	    return result;
	}
	return null;
    }

    @Override
    public boolean validate(ClickEvent event) {
	return true;
    }

}
